PIDFILE=${buildout:directory}/bin/gunicorn.pid

echo $PIDFILE

case "$1" in
    "stop" )
        if [ -f $PIDFILE ]; then
            kill `cat -- $PIDFILE`
            rm -f -- $PIDFILE
        fi    
    ;;
    "" | "fg" )
        if [ -f $PIDFILE ]; then
            kill `cat -- $PIDFILE`
            rm -f -- $PIDFILE
        fi

        if [ "$1" = "fg" ]; then
            ${buildout:directory}/bin/django run_gunicorn 127.0.0.1:${django-gunicorn:port} --workers=${django-gunicorn:workers}
        else
            ${buildout:directory}/bin/django run_gunicorn 127.0.0.1:${django-gunicorn:port} --pid=$PIDFILE --workers=${django-gunicorn:workers} --daemon
        fi
esac